package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.PaasCommentDao;
import com.xbongbong.paas.domain.entity.PaasCommentEntity;
import com.xbongbong.paas.domain.entity.ext.PaasCommentEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.PaasCommentModel;
import com.xbongbong.paas.pojo.dto.ReplyCountDTO;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.BusinessCommentEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: wufeng
 * @date: 2018/8/29 10:05
 * @desrcption:
 */
@Service("paasCommentModel")
public class PaasCommentModelImpl implements PaasCommentModel {

    private static final String COLUMNS = " id, corpid, app_id, ref_id, type, user_id, user_name, user_avatar, ref_user_id, ref_user_name, at_user_id, content, image, attachment, add_time, update_time, del ";


    @Resource
    private PaasCommentDao paasCommentDao;

    @Override
    public Integer insert(PaasCommentEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return paasCommentDao.insert(entity);
    }

    @Override
    public List<PaasCommentEntityExt> list(Map<String, Object> map) {
        map.put("columns", new StringBuffer(COLUMNS));
        return paasCommentDao.findEntitys(map);
    }

    @Override
    public List<PaasCommentEntityExt> findEntitys(Map<String, Object> map) {
        return paasCommentDao.findEntitys(map);
    }

    @Override
    public List<PaasCommentEntityExt> insertBatch(List<PaasCommentEntityExt> entityList) {
        paasCommentDao.insertBatch(entityList);
        return entityList;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return paasCommentDao.getEntitysCount(map);
    }

    @Override
    public PaasCommentEntity getByKey(Long key, String corpid) {
        return paasCommentDao.getByKey(key, corpid);
    }

    @Override
    public Map<Long, Integer> getCommnetCountMap(Collection<Long> dataIdIn, String corpid, Integer code) {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("type", code);
        map.put("del", 0);
        map.put("refIdIn", dataIdIn);
        List<PaasCommentEntityExt> paasCommentEntityList = list(map);
        Map<Long, Integer> commentCountMap = new HashMap<>(paasCommentEntityList.size());
        for (PaasCommentEntityExt entityExt : paasCommentEntityList) {
            commentCountMap.put(entityExt.getRefId(), commentCountMap.getOrDefault(entityExt.getRefId(), 0) + 1);
        }
        return commentCountMap;
    }

    @Override
    public List<Long> hasMyComment(Collection<Long> dataIdIn, String corpid, Integer type, String userId) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("refIdIn",dataIdIn);
        map.put("type",type);
        map.put("userId",userId);
        map.put("del",0);
        map.put("groupByStr","ref_id");
        map.put("columns","ref_id");
        List<PaasCommentEntityExt> paasCommentEntityExtList = list(map);
        List<Long> refIdList = new ArrayList<>();
        for (PaasCommentEntityExt entityExt : paasCommentEntityExtList){
            refIdList.add(entityExt.getRefId());
        }
        return refIdList;
    }

    @Override
    public Integer dataConsistencyUpdateUser(String corpid, String userId, String userName, String userAvatar) {
        return paasCommentDao.dataConsistencyUpdateUser(corpid, userId, userName, userAvatar);
    }

    @Override
    public Integer dataConsistencyUpdateRefUser(String corpid, String userId, String userName) {
        return paasCommentDao.dataConsistencyUpdateRefUser(corpid, userId, userName);
    }

    @Override
    public Integer deleteByKey(Long id, String corpid){
        return paasCommentDao.deleteByKey(id,corpid);
    }

    @Override
    public Integer deleteByRefId(Long refId, String corpid, Long appId, Integer type){
        return  paasCommentDao.deleteByRefId(refId,corpid,appId,type);
    }

    @Override
    public Integer updateContentByKey(PaasCommentEntity entity){
        return paasCommentDao.update(entity);
    }

    @Override
    public Map<Long, Integer> batchGetReplyCount(String corpid,Integer replyType, List<Long> commentIdList) {
        Map<Long, Integer> replyCountMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (CollectionUtils.isEmpty(commentIdList)) {
            return replyCountMap;
        }

        List<ReplyCountDTO> replyCountDTOList = paasCommentDao.batchGetReplyCount(corpid, replyType, commentIdList);
        replyCountMap = replyCountDTOList.stream().collect(Collectors.toMap(item -> item.getCommentId(), item -> item.getReplyCount()));

        return replyCountMap;
    }

    @Override
    public void deleteCommentWithReply(Long commentId, String corpid, Integer replyType,Long appId) {
        // 评论删除
        paasCommentDao.deleteByKey(commentId,corpid);
        // 评论下回复数据删除
        paasCommentDao.deleteByRefId(commentId,corpid,appId,replyType);
    }
}
